//
//  HomeItemsView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/20.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

protocol HomeItemsViewDelegate:NSObjectProtocol {
    func HomeItemsViewClick(target:String, name:String, clickUrl:String)
}

class HomeItemsView: UIView {
    

    weak var delegate:HomeItemsViewDelegate?
    let HomeItemsCellId = "HomeItemsCellId"
//    fileprivate var viewCT:UIViewController!
    
    var model: [CentreItemImagesModel]?
    var modelList: [CentreItemImagesModel]? {
        didSet {
            guard let model = modelList else { return }
            self.model = model
            self.collectionView.reloadData()
        }
    }
    
    fileprivate lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
//        layout.itemSize = CGSize.init(width: SCREEN_WIDTH / 5, height: 70)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.register(HomeItemsCell.self, forCellWithReuseIdentifier: HomeItemsCellId)
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()
    
//    var itemsModel:[CentreItemImagesModel]
    
    init(frame: CGRect, viewController:UIViewController) {
        super.init(frame: frame)
        self.collectionView.frame = frame
        self.collectionView.backgroundColor = WhiteBackColor
        self.addSubview(self.collectionView)
//        self.viewCT = viewController
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//  MARK: - ###### 私有方法 ######
extension HomeItemsView {
    func setCollectionView() {
        self.value(forKey: )
    }
        
}
    

//  MARK: - ###### 公有方法 ######
extension HomeItemsView {
   
}

//  MARK: - ###### 协议方法 ######
extension HomeItemsView:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.model?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HomeItemsCellId, for: indexPath) as! HomeItemsCell
        cell.imageView.sd_setImage(with: URL(string: StringIncludeChineseToString(self.model?[indexPath.row].pic ?? "")), completed: nil)
        cell.label.text = self.model?[indexPath.row].name
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let clickUrl:String = self.model?[indexPath.row].clickUrl ?? "0"
        let target:String = self.model?[indexPath.row].target ?? "0"
        let name:String = self.model?[indexPath.row].name ?? ""
        delegate?.HomeItemsViewClick(target: target, name: name, clickUrl: clickUrl)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: SCREEN_WIDTH / CGFloat(self.model?.count ?? 0), height: 70)
    }

}

//  MARK: - ###### 私有方法 ######
extension HomeItemsView {
    
}
